# planting_records/admin.py
from django.contrib import admin
from .models import PlantingRecord

@admin.register(PlantingRecord)
class PlantingRecordAdmin(admin.ModelAdmin):
    list_display = ('field', 'rice_variety', 'planting_date', 'planting_method', 'owner', 'updated_at')
    list_filter = ('planting_method', 'owner', 'field__name', 'planting_date')
    search_fields = ('rice_variety', 'field__name', 'notes', 'owner__username')
    autocomplete_fields = ['field', 'owner'] # Makes selecting ForeignKey easier if many exist
    date_hierarchy = 'planting_date' # Adds date drilldown navigation
    readonly_fields = ('created_at', 'updated_at')
    fieldsets = (
        (None, {'fields': ('owner', 'field', 'rice_variety', 'planting_date', 'planting_method')}),
        ('可选信息', {'fields': ('seedling_source', 'expected_harvest_date', 'notes'), 'classes': ('collapse',)}),
        ('时间戳', {'fields': ('created_at', 'updated_at'), 'classes': ('collapse',)}),
    )

    # Optimize query for owner/field display
    def get_queryset(self, request):
        return super().get_queryset(request).select_related('owner', 'field')